package edu.ccut.saturn.component.loader.node;

/**
 * 用于解析logic组件、action组件配置文件中“开始”结点的加载类。
 * <p>继承了<code>NodeInfoLoader</code>,并实现了<code>loadNode</code>方法。<p>
 * 
 * @author 谷钰
 * @version 1.0 2008.8.18
 * 
 * @see <code>StartNodeInfo</code>
 */
import java.util.LinkedList;
import java.util.List;

import org.dom4j.Element;
import org.osgi.framework.Bundle;

import edu.ccut.saturn.component.INodeInfo;
import edu.ccut.saturn.component.IParameterInfo;
import edu.ccut.saturn.component.SaturnComponentException;
import edu.ccut.saturn.component.impl.node.StartNodeInfo;

public class StartNodeInfoLoader extends NodeInfoLoader {

	public static final String KEY_NODETYPE_START = "start";

	public static final String KEY_NODE_START_TRANSACTION = "transaction";

	public static final String KEY_NODE_START_TRANSACTION_AUTOROLLBACK = "autorollback";

	public List<INodeInfo> loadNode(Element root, Bundle bundle)
			throws SaturnComponentException {

		try {
			Element start = root.element(KEY_NODETYPE_START);
			String id = start.attributeValue(KEY_NODE_ID);
			String x = start.attributeValue(KEY_NODE_X).trim();
			String y = start.attributeValue(KEY_NODE_Y).trim();
			String name = start.elementTextTrim(KEY_NODE_NAME);
			String description = start.elementTextTrim(KEY_NODE_DESCRIPTION);
			String nextId = start.attributeValue(KEY_NODE_NEXT).trim();
			Element transaction = start.element(KEY_NODE_START_TRANSACTION);
			String autoRollBack = null;

			if (transaction != null) {
				autoRollBack = transaction
						.attributeValue(KEY_NODE_START_TRANSACTION_AUTOROLLBACK);
			}

			if (autoRollBack == null || "".equals(autoRollBack)) {
				autoRollBack = "false";
			}
			List<IParameterInfo> parameters = getParameterInfos(bundle, start);
			
			logger.debug("  load start node name = \"" + name + "\" description = \"" + description + "\" nextId = \"" + nextId + "\"");
			
			List<INodeInfo> nodes = new LinkedList<INodeInfo>();
			nodes.add(new StartNodeInfo(id, name, description, nextId,
					parameters, autoRollBack, x, y));

			return nodes;

		} catch (Exception e) {
			// 如果解析xml出现异常，对整个xml文件的解析将没有意思，所以不在继续解析
			throw new SaturnComponentException("load start node failed!", e);
		}
	}
}
